
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');

const baseUrl = process.env.PUBLIC_URL ? `${process.env.PUBLIC_URL}/${process.env.STATIC_EXTRA_PATH}` : '';


console.log('process.env', process.env.NODE_ENV)



module.exports = {
  // @todo 变成自动读取
  entry: {
    entry: path.resolve(__dirname, '../src/index.tsx')
  },
  // output: {
  //   filename: '[name].js',
  //   path: path.resolve(__dirname, '../dist'),
  //   clean: true,
  //   publicPath: '',
  //   libraryTarget: 'umd',
  //   // filename: 'js/[name]_[chunkhash:8].js',
  //   // chunkFilename: 'js/[name]_[chunkhash:8].js',
  // },
  externals: {
    // react: 'react',
    // 'react-dom': 'react-dom',
    // 'react-dom/server': 'react-dom/server',
    // moment: 'moment',
    // antd: 'antd',
    // lodash: 'lodash',
    // i18next: 'i18next',
    // 'react-i18next': 'react-i18next',
  },
  stats: 'errors-warnings',
  resolve: {
    extensions: ['.ts', '.tsx', '.js', '.jsx'],
    alias: {
      '@': path.resolve(__dirname, '../src'),
      
    },
  },
  module: {
    rules: [
      {
        test: /\.js?$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
     
      },
      {
      test: /\.tsx?$/,
      exclude: /node_modules/,
      loader: 'ts-loader',
      options: {
        transpileOnly: true,
       
      },
    },
    {
      test: /\.less$/,
      // exclude: /node_modules/,
      use: [
        {
          loader: 'style-loader',
        },
        {
          loader: 'css-loader',
        },
        {
          loader: 'less-loader',
          options: {
            lessOptions:{
              javascriptEnabled: true,
              modifyVars: {
                '@base-font-size': 37.5,
            }
            }
          }
        }],
    },
    {
      test: /\.(jpg|png|gif|bmp|jpeg)$/,
      use: [
        {
          loader: 'url-loader',
          options: {
            outputPath: '/static/resource/img/',
            limit: 8192,
          },
        },
      ],
    },
    {
      test: /\.css$/,
      use: ["style-loader", "css-loader", "postcss-loader"],
    },
    {
      test: /\.svg$/,
      exclude: /node_modules/,
      loader: '@svgr/webpack'
    }
    ],
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env': JSON.stringify({
        ...process.env,
      })
    }),
    new HtmlWebpackPlugin({
      filename: path.resolve(__dirname, '../dist/main/index.html'),
      template: path.resolve(__dirname, `../public/index.html`),
      templateParameters: {
        entryPath: `${baseUrl}/entry.js?v=${Number(new Date())}`,
      },
      inject: false, // @todo 'body',
      // templateContent: {}, // function() {}
    }),
    // new ForkTsCheckerWebpackPlugin({
    //   eslint: {
    //     files: './src/**/*.{ts,tsx,js,jsx}',
    //   },
    // }),
  ],
  optimization: {
    usedExports: false,
  }
};
